AsynchroSerial
 
 
 Bean AsynchroSerial
 
Asynchronous serial communication

Typical usage of the bean in user's code.

Typical Usage:

Required bean name is "AS1".
There are the following four typical usage modes:

(1)
The simplest usage of the bean is a communication without interrupts (polling mode).

 MAIN.C

byte ch;
byte err;
AS1_TError error;
void main(void)
{
  bool b;

  for(;;) {
    //Wait for a character
    while(AS1_GetCharsInRxBuf() == 0);

    //Read received character and send it if no error is detected
    if( AS1_RecvChar(&ch) == ERR_OK) {
      AS1_SendChar(ch);
    } else {
      // The GetBreak method must be used first,
      // because the GetError method clears the break flag
      // This method is available if the Break
      // signal is enabled (Advanced view).
AS1_GetBreak(&b);
      if (b) {
        // the break character was received
      }
      AS1_GetError(&error)
      if(error.errName.Parity)
        //Parity error
      else if(error.errName.Framing)
        //Framing error
      .
      .
      .
    }
  }
}

(2)
The next typical usage of this bean is a communication with interrupts using communication events.

 EVENTS.C

void AS1_OnRxChar(void)
{
  byte ch;

  //Read received character and send it if no error is detected
  if(AS1_RecvChar(&ch) == ERR_OK)
    AS1_SendChar(ch);

}

(3)
Following example shows how to recognize a communication error using the GetError method. In the interrupt communication mode the user can use the OnError event to recognize an error during the communication.

 MAIN.C

byte ch;
AS1_TError error;	//TError union is defined in the header file
void main(void)
{

  //Wait for any character
  while(AS1_GetCharsInRxBuf() == 0);

  //Read received character
  AS1_RecvChar(&ch);

  //Read the last communication errors
  AS1_GetError(&error)
  if(error.err == 0)
    //Send the last received character if no error is detected
    AS1_SendChar(ch);
  else
    if(error.errName.Parity)
      //Parity error
    else if(error.errName.Framing)

      //Framing error
    .
    .
    .
    .
}

(4)
The next typical usage of this bean is a communication using DMA transfer. Interrupts can be enabled or disabled. This typical usage can be used by the derivatives with a DMA controller.

 MAIN.C
byte ch;
AS1_TError error;        //TError union is defined in the header file
void main(void)
{

  //Start DMA request (DMA request have to be specified first!)
  AS1_RecvChar(&ch);

  //Wait for any character
  while(AS1_GetCharsInRxBuf() == 0);

  //Read the last communication errors
  AS1_GetError(&error)
  if(error.err == 0)
    //Send the last received character if no error is detected
    AS1_SendChar(ch);
  else
    if(error.errName.Parity)
      //Parity error
    else if(error.errName.Framing)
      //Framing error
    .
    .
    .
}

 EVENTS.C

void AS1_OnFullRxBuf(void)
{
  //Requested data have been received. DMA transfer is finished.
}

void AS1_OnFreeTxBuf(void)
{
  //Requested data have been sent. DMA transfer is finished.
}

(5)

Typical usage of block transfer functions. In this example the bean will be used to receive and send fixed size packets of data. The structure of the packet is - 1B header, 1B command, 2B data, 1B checksum

Suppose the following bean settings:

  • Interrupt service: Enabled
  • Input buffer size: 5
  • Output buffer size: 5
  • Methods enabled: SendBlock, RecvBlock
  • EventsEnabled: OnFullRxBuf

Definition of the packet structure:

struct {
  byte Header;
  byte Command;
  word Data;
  byte Checksum;
} message;

A data packet fills the receive buffer, so the OnFullRxBuf event is invoked when a data packet is received. The received packet may be read using the RecvBlock method.

Example of the OnFullRxBuf event code:

  void AS1_OnFullRxBuf(void)
  {
    word Received;
    byte err;
    err = AS1_RecvBlock((byte*)&message, sizeof(message), &Received);
    ....
  }
To send a data packet the SendBlock method may be used:
byte err;
word Sent;
void main(void)
{
  ...
  message.Header = 0x55;
  message.Command = 0x01;
  message.Data = 0x0A0B;
  message.Checksum = message.Header + message.Command + (message.Data & 0x00ff) + (message.Data >> 8);
  err = AS1_SendBlock((byte*)&message, sizeof(message), &Sent);
  ....
}


(6)

Multiple baud rates selection at runtime

The AsynchroSerial bean allows user to change baud rate value at runtime.

In the timing dialog of the Baud rate property set runtime setting type to "from list of values". Then it is possible to enter up to 16 different values. The SetBaudRateMode method is used to select the actual baud rate. The bean generates a special symbolic constant for each baud rate value defined (see SetBaudRateMode), e.g. AS1_BM_2400BAUD, AS1_BM_4800BAUD, etc. The constant is used as a parameter of the SetBaudRateMode method.

void main(void) {
  ...
  AS1_SetBaudRateMode(AS1_BM_2400BAUD)
  ...
  ...
  AS1_SetBaudRateMode(AS1_BM_4800BAUD)
  ..
}

For more about typical usage of the bean code please refer to the page Bean Code Typical Usage.


Processor ExpertTM and Embedded BeansTM are registered trademarks of UNIS, Ltd.
©1997-2005, UNIS, Ltd.